﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace PickGold.Attributes
{
	/// <summary>
	/// 指示默认实例获取成员属性或方法
	/// </summary>
	[Serializable]
	[ComVisible(true)]
	[ClassInterface(ClassInterfaceType.AutoDispatch)]
	[AttributeUsage(AttributeTargets.Constructor | AttributeTargets.Method | AttributeTargets.Property | AttributeTargets.Event | AttributeTargets.Delegate)]
	public class DelegateRunnerAttribute : Attribute
	{
		private string _AssemblyName;
		private string _TypeName;

		/// <summary>
		/// 实例化实例获取指示器成员
		/// </summary>
		/// <param name="type">对象类型</param>
		public DelegateRunnerAttribute(Type type)
		{
			this._AssemblyName = type.Assembly.FullName;
			this._TypeName = type.FullName;
		}

		/// <summary>
		/// 对象的程序集名称
		/// </summary>
		public string AssemblyName
		{
			get
			{
				return this._AssemblyName;
			}
		}

		/// <summary>
		/// 对象类型名称
		/// </summary>
		public string TypeName
		{
			get
			{
				return this._TypeName;
			}
		}

		/// <summary>
		/// 当在派生类中重写时，返回一个指示此实例是否等于指定对象的值。
		/// </summary>
		/// <param name="obj">与 System.Attribute 的此实例进行比较的 System.Object。</param>
		/// <returns>如果该实例等于 obj，则为 true；否则，为 false。</returns>
		public override bool Match(object obj)
		{
			DelegateRunnerAttribute a = obj as DelegateRunnerAttribute;
			if (a == null)
				return base.Match(obj);

			if (this._AssemblyName != a._AssemblyName)
				return false;

			if (this._TypeName != a._TypeName)
				return false;

			return true;
		}

		/// <summary>
		/// 返回一个值，该值指示此实例是否与指定的对象相等。
		/// </summary>
		/// <param name="obj">要与此实例进行比较的 System.Object 或 null。</param>
		/// <returns>如果 obj 等于此实例的类型和值，则为 true；否则为 false。</returns>
		public override bool Equals(object obj)
		{
			DelegateRunnerAttribute a = obj as DelegateRunnerAttribute;
			if (a == null)
				return base.Equals(obj);

			if (this._AssemblyName != a._AssemblyName)
				return false;

			if (this._TypeName != a._TypeName)
				return false;

			return true;
		}

		/// <summary>
		/// 返回该字符串的哈希代码。
		/// </summary>
		/// <returns>32 位有符号整数哈希代码。</returns>
		public override int GetHashCode()
		{
			string t = this._AssemblyName + Environment.NewLine + this._TypeName;
			return t.GetHashCode();
		}
	}
}
